package com.example.a2209_shop.register

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.a2209_shop.R
import com.example.a2209_shop.databinding.ActivityRegisterBinding
import com.example.base.mvvm.BaseActivity

@Route(path = "/app/RegisterActivity")
class RegisterActivity : BaseActivity<ActivityRegisterBinding, RegisterViewModel>() {
    override fun initData() {
        viewModel.registerSuccess.observe(this) {
            if (it.code == 200) {
                //跳转到登录界面---带用户名密码跳转
                ARouter.getInstance().build("/app/LoginActivity").withString("username",it.data.username)
                    .withString("password",it.data.password).navigation()
            }
        }
    }

    override fun initView() {
        binding.register.setOnClickListener {
            val username = binding.etName.text.toString()
            val password = binding.etPwd.text.toString()
            val repassword = binding.etRepwd.text.toString()
            if(username.isEmpty()||password.isEmpty()||repassword.isEmpty()){
                ToastUtils.showLong("用户名和密码不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileSimple(username)){
                ToastUtils.showLong("用户名格式不正确，应为手机号")
                return@setOnClickListener
            }
            if(!RegexUtils.isMatch("^[\\w]{6,8}$",password)){
                ToastUtils.showLong("密码格式不正确，应为6-8位的数字、字母、下划线的组合")
                return@setOnClickListener
            }
            if(password!=repassword){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            if(!binding.fwb.isChecked){
                ToastUtils.showLong("您未勾选同意协议")
                return@setOnClickListener
            }
            viewModel.register(
                binding.etName.text.toString(),
                binding.etPwd.text.toString(),
                binding.etRepwd.text.toString()
            )
        }
        //设置富文本
        val builder = SpannableStringBuilder(binding.fwb.text.toString())
        val clickableSpan = object :ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebViewActivity2").withString("url","http://www.baidu.com").navigation()
            }
        }

        builder.setSpan(clickableSpan,9,13,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        builder.setSpan(ForegroundColorSpan(Color.RED),9,13,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.fwb.movementMethod = LinkMovementMethod.getInstance()
        binding.fwb.text = builder
    }

    override fun getViewModel(): Class<RegisterViewModel> = RegisterViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_register

}